package com.atguigu.gmall.list.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.service.ListService;
import com.atguigu.gmall.model.list.Goods;
import com.atguigu.gmall.model.list.SearchParam;
import com.atguigu.gmall.model.list.SearchResponseVo;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;


/*全文检索请求*/
@RestController
@RequestMapping("api/list")
public class ListApiController {

    //es封装
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    //spring封装
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private ListService listService;

    //创建索引
    @GetMapping("/creatIndex")
    public Result creatIndex(){
        //1、创建索引    2、type    3、Mapping
        elasticsearchRestTemplate.createIndex(Goods.class);
        elasticsearchRestTemplate.putMapping(Goods.class);

        return Result.ok();

    }

    //根据skuId  导入索引
    @GetMapping("/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable Long skuId){
        listService.upperGoods(skuId);
        return Result.ok();
    }

    //根据skuId  删除索引
    @GetMapping("/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable Long skuId){
        listService.lowerGoods(skuId);
        return Result.ok();
    }

    //增加热度 评分
    @GetMapping("/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok();
    }


    //开始搜索es索引库，为显示搜索后的页面
    @PostMapping("/search")
    public SearchResponseVo search(@RequestBody SearchParam searchParam){

        //DSL
        SearchResponseVo vo =  listService.search(searchParam);

        return vo;
    }













}
